package mop.gui.main;

import java.util.LinkedList;
import mop.controller.LoginUserController;
import mop.gui.GUI;
import mop.model.user.UserInterface;

/**
 * LoginDialog
 * 
 * @author  MOP Team 7
 */
public class LoginDialog extends javax.swing.JDialog
{
    /**
     * Constructor.
     * 
     * @param   gui
     * @param   loginUserController
     */
    public LoginDialog(GUI gui, LoginUserController loginUserController)
    {
        super(new javax.swing.JFrame(), true);
        initComponents();
        setGui(gui);
        
        this.loginUserController = loginUserController;
        
        loadUsers();
    }
    
    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/
    
    private GUI gui;
    
    /**
     * Sets the GUI.
     * 
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }
    
    /**
     * Returns the GUI reference.
     * 
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }
    
    /***************************************************************************
     *                          Controllers
     ***************************************************************************/
    
    private LoginUserController loginUserController;
    
    /***************************************************************************
     *                          Dialog-specific functionality
     ***************************************************************************/
    
    /**
     * Loads the userlist into the combobox.
     */
    public void loadUsers()
    {
        LinkedList<UserInterface> users = (LinkedList<UserInterface>) loginUserController.getUserList();
        for(UserInterface u : users)
            this.cmbUsers.addItem(u.getId());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitle = new javax.swing.JLabel();
        lblCredits = new javax.swing.JLabel();
        lblUser = new javax.swing.JLabel();
        cmbUsers = new javax.swing.JComboBox();
        btnLogin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Log in");

        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblTitle.setText("MOP Project 2009-2010 - Iteratie 3");
        lblTitle.setName("lblTitle"); // NOI18N

        lblCredits.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCredits.setText("<html> Adriaan Larmuseau<br>  Laurens Van Keer<br> Seba Kerckhof<br> </html>"); // NOI18N
        lblCredits.setName("lblCredits"); // NOI18N

        lblUser.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblUser.setText("User");
        lblUser.setName("lblUser"); // NOI18N

        cmbUsers.setName("cmbUsers"); // NOI18N

        btnLogin.setText("Log In");
        btnLogin.setName("btnLogin"); // NOI18N
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCredits, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(lblTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblUser)
                        .addGap(18, 18, 18)
                        .addComponent(cmbUsers, 0, 336, Short.MAX_VALUE))
                    .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCredits, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUser)
                    .addComponent(cmbUsers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnLogin)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

/**
 * Logs the selected user in.
 * 
 * @param   evt
 */
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
    String userName = (String) this.cmbUsers.getSelectedItem();
    
    try {
        loginUserController.loginUser(userName);
        gui.handleSuccess("Logged in!");
        gui.showMainWindow();
        this.dispose();
    } catch (Throwable exception) {
        gui.handleError(exception);
    }
}//GEN-LAST:event_btnLoginActionPerformed

/**
 * Opens the "create user" dialog.
 * 
 * @param   evt
 */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JComboBox cmbUsers;
    private javax.swing.JLabel lblCredits;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblUser;
    // End of variables declaration//GEN-END:variables
}